/*******************************************************************************
 * Copyright (c) 2010, 2030 www.itlaobing.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package cn.itlaobing.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import cn.itlaobing.model.UserModel;


/** 
 * ClassName: UserDao <br/> 
 * Function: 用户dao层 <br/> 
 * date: 2017年11月1日 下午2:36:06 <br/> 
 * 
 * @author wd
 * @version  
 * @since JDK 1.8 
 */
public class UserDao extends BaseDao<UserDao> {
	
		public int addUser(UserModel um)throws ClassNotFoundException,SQLException{
			String sql="insert into users values(null,?,?,?,?,?)";
			return dbhelper.executeUpdate(sql, um.getUserName(),um.getUserPass(),um.getLastDate(),um.getNickName(),um.getUserPhone());
		}
	
		public int delUser(int userid)throws SQLException,ClassNotFoundException{
			String sql="delete from users where userid=?";
			return dbhelper.executeUpdate(sql, userid);
		}
		
		public Boolean findUserByName(String name)throws SQLException,ClassNotFoundException{
			String sql="select count(1) from users where username=?";
			Boolean f=false;
			ResultSet rs=dbhelper.executeQuery(sql, name);
			if(rs.next()) {
				f=rs.getInt(2)==0?false:true;
			}
			return f;
		}
	
		public UserModel findUserByNameAndPwd(String name,String pwd)throws ClassNotFoundException,SQLException{
			String sql="SELECT * FROM users WHERE username=? AND userpass=?";
			UserModel um=null;
			ResultSet rs=dbhelper.executeQuery(sql, name,pwd);
			if(rs.next()) {
				um=new UserModel(rs.getInt(1),rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6));
			}
			return um;
		}
}
